package com.boarsoft.boar.admin.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.admin.DeptBiz;
import com.boarsoft.boar.dir.action.TreeAction;
import com.boarsoft.boar.dir.bean.DirTreeNode;
import com.boarsoft.boar.entity.DirInfo;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;

@Component
@RestController
@RequestMapping("/dept")
public class DeptAction extends TreeAction {
	@Autowired
	protected DeptBiz deptBiz;

	/**
	 * 分页查询所有的公司部门
	 * @return 
	 */
	@RequestMapping("/list.do")
	@Authorized(code = "dept.list")
	public ReplyInfo<Object> list(String id, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<DirInfo> pr = dirBiz.list(id, key, orderBy, pageNo, pageSize);
		return new ReplyInfo<Object>(true, pr);
	}

	@RequestMapping("/save.do")
	@Authorized(code = "dept.save")
	public ReplyInfo<Object> save(String id, String parent, short type, String code, String name, String memo) {
		ReplyInfo<Object> ro = this.save(id, parent, type, code, name, memo);
		if (!ro.isSuccess()) {
			return new ReplyInfo<Object>(true, String.valueOf(ro.getData()));
		}
		if (ro.isSuccess()) {
			DirTreeNode dtn = new DirTreeNode((DirInfo) ro.getData());
			return new ReplyInfo<Object>(true, JsonUtil.from(dtn));
		} else {
			return new ReplyInfo<Object>(String.valueOf(ro.getData()));
		}
	}

	/**
	 * 仅当不使用树来管理时使用
	 * @return 
	 */
	@RequestMapping("/delete.do")
	@Authorized(code = "dept.delete")
	public ReplyInfo<Object> delete(String id) {
		deptBiz.delete(id);
		return ReplyInfo.SUCCESS;
	}
}